package config

import (
	"io"
	"log"
	"os"

	"gopkg.in/yaml.v3"
)

var CF Config

type Config struct {
	Mysql    `yaml:"mysql"`
	Listener `yaml:"listener"`
	Logging  `yaml:"logging"`
	Redis    `yaml:"redis"`
	Jwt      `yaml:"jwt"`
}

type Logging struct {
	Level  string `yaml:"level"`
	Stdout string `yaml:"stdout"`
	Stderr string `yaml:"stderr"`
}

type Redis struct {
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	Password string `yaml:"password"`
	Database int    `yaml:"database"`
}

type Mysql struct {
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Database string `yaml:"database"`
}

type Listener struct {
	Host string `yaml:"host"`
	Port int    `yaml:"port"`
}

type Jwt struct {
	Secret string `yaml:"secret"`
}

func Init() {
	f, err := os.OpenFile("config/config.yaml", os.O_RDONLY, 0)
	if err != nil {
		log.Fatalln(err)
	}
	data, err := io.ReadAll(f)
	if err != nil {
		log.Fatalln(err)
	}

	err = yaml.Unmarshal(data, &CF)
	if err != nil {
		log.Fatalln(err)
	}

}
